import lombok.Data;

import java.util.HashSet;

public class N50人排队遇到3移除问题 {


    @Data
    private static class User {
        private int data;
        private User next;

        @Override
        public String toString() {
            return "User{" +
                    "data=" + data +
                    '}';
        }
    }

    public static User initUserNode() {
        User userHead = new User();
        userHead.next = null;
        User tmp = null;
        User cur = userHead;
        for (int i = 1; i <= 7; i++) {
            tmp = new User();
            tmp.data = i;
            if (i == 7) {
                tmp.next = userHead;
            } else {
                tmp.next = null;
            }
            cur.next = tmp;
            cur = tmp;
        }
        return userHead;
    }

    public static void main(String[] args) {
        User user = initUserNode();
        int count = 0;
        User cur = user;
        while (cur != null && getLen(user) > 2) {
            if (count == 2) {
                User next1 = cur.next.next;
                user.next = next1;
                count = 0;
            } else {
                count++;
            }
            cur = cur.next;
        }
        System.out.println(user);
    }

    public static int getLen(User user) {
        int i = 0;
        HashSet set = new HashSet();
        User user1 = user;
        while (user1 != null && !set.contains(user1.data)) {
            set.add(user1.data);
            user1 = user1.next;
            i++;
        }
        return i;
    }
}
